package com.joe.utils;

import com.joe.entity.Po.ParamType;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by joe on 22:07
 */
public class ReflectUtils {

    private static final Map<Class, Field[]> fieldCache = new HashMap<>();

    public static Field[] getAllFields(Class<?> clazz) {
        Field[] fields = fieldCache.get(clazz);
        if (fields != null) {
            return fields;
        }
        return null;
    }

    /**
     * 获取 method 返回值类型
     */
    public static ParamType getReturnType(Method method) {
        ParamType paramType = null;
        // list 结果处理
        Type type = method.getGenericReturnType();
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Class<?> actualClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
            Class<?> collectionClass = (Class<?>) parameterizedType.getRawType();

            paramType = new ParamType(collectionClass, actualClass);
        }
        return paramType;
    }

    /**
     * 查找并返回class所有的field
     */
    private static Field[] findAllFields(Class clazz) {
        return null;
    }


}
